<?php
function buildMenu($id, $type)
{
	$output = "";
	$result = mysql_query('SELECT * FROM '.DB_PERFIX.'menu_templates WHERE template_id='.$id.' AND level=0 LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{ 
		$output = '<table width="100%" cellpadding="0" cellspacing="0" border="0" style="'.$row['style'].'"  >';
		$output = $output.'<tr>';
		if($row['image_l'] != '')
		{
			$output = $output.'<td width="0" height="'.$row['height'].'"><img src="../images/templates/menu/'.$row['image_l'].'" border="0"></td>';
		}
		$output = $output.'<td height="'.$row['height'].'" width="100%" class="menu_type_'.$row['template_id'].'_txt"  style="background-image: url(../images/templates/menu/'.$row['image_bg'].'); background-repeat: repeat-x;">';
		
		$lev2 = mysql_query('SELECT * FROM '.DB_PERFIX.'menu_templates WHERE template_id='.$id.' AND level=1 LIMIT 1;');
		while($level2 = mysql_fetch_array($lev2))
		{
			$output = $output.'<table width="100%" cellpadding="0" cellspacing="0" border="0" style="'.$level2['style'].'">';
			$output = $output.'<tr>';
			
			for($i = 0; $i<6; $i++)
			{
				$width=(100/6);
				if($i == 5)
				{	
					$class=2;
				}
				else
				{
					if($i < 1)
					{ $class=0; }
					else
					{ $class=1; }
				}
				if($level2['image_l'] != '')
				{
					$output = $output.'<td width="0" height="'.$level2['height'].'"><img src="../images/templates/menu/'.$level2['image_l'].'" border="0"></td>';
				}
				if($i==2)
				{
					$output = $output.'<td 	height="'.$level2['height'].'" 
											onMouseOver="this.style.backgroundImage=\'url(../images/templates/menu/'.$level2['image_hov'].')\'; 
														 this.className=\'menu_type_'.$row['template_id'].'_1_'.$class.'_hov\';" 
											onMouseOut="this.style.backgroundImage=\'url(../images/templates/menu/'.$level2['image_act'].')\'; 
														this.className=\'menu_type_'.$row['template_id'].'_1_'.$class.'_act\';" 
											align="center" 
											width="'.$width.'%" 
											class="menu_type_'.$row['template_id'].'_1_'.$class.'_act" 
											style="background-image: url(../images/templates/menu/'.$level2['image_act'].'); 
											background-repeat: repeat-x; cursor: pointer;">';
					$active= 'active ';
				}
				else
				{	
					$output = $output.'<td 	height="'.$level2['height'].'" 
											onMouseOver="this.style.backgroundImage=\'url(../images/templates/menu/'.$level2['image_hov'].')\'; 
														 this.className=\'menu_type_'.$row['template_id'].'_1_'.$class.'_hov\';" 
											onMouseOut="this.style.backgroundImage=\'url(../images/templates/menu/'.$level2['image_bg'].')\'; 
														this.className=\'menu_type_'.$row['template_id'].'_1_'.$class.'\';" 
											align="center" 
											width="'.$width.'%" 
											class="menu_type_'.$row['template_id'].'_1_'.$class.'" 
											style="background-image: url(../images/templates/menu/'.$level2['image_bg'].'); 
											background-repeat: repeat-x; cursor: pointer;">';
					$active= '';
				}
				$output = $output.'Link '.$active.$i;
				$output = $output.'</td>';
				if($level2['image_r'] != '')
				{
					$output = $output.'<td width="0" height="'.$level2['height'].'"><img src="../images/templates/menu/'.$level2['image_r'].'" border="0"></td>';
				}
			}
			
			$output = $output.'</tr>';
			$output = $output.'</table>';
		}
		
		$output = $output.'</td>';
		if($row['image_r'] != '')
		{
			$output = $output.'<td width="0" height="'.$row['height'].'"><img src="../images/templates/menu/'.$row['image_r'].'" border="0"></td>';
		}
	}
	
	$output = $output.'</tr>';
	$output = $output.'</table>';
	return $output;
}
function addMenuTemplate($name, $type)
{		
	$result = mysql_query('SELECT template_id FROM '.DB_PERFIX.'menu_templates ORDER BY template_id DESC LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{ $item['template_id'] = $row['template_id']; }
	$item['template_id']++;
	
	
	$insert  = 'INSERT INTO '.DB_PERFIX.'menu_templates (`template_id`, `name`, `type`)'; 
	$insert .= 'VALUES ('.$item['template_id'].', "'.$name.'", "'.$type.'");';
	$result = mysql_query($insert);
	
	echo $insert;
	$result = mysql_query('SELECT id FROM '.DB_PERFIX.'menu_templates WHERE level=0 ORDER BY id DESC LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{ $item['id'] = $row['id']; }
	return $item['id'];
}
function addMenuLevel($id)
{		
	$result = mysql_query('SELECT * FROM '.DB_PERFIX.'menu_templates WHERE template_id='.$id.' AND level=0 LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{
		$menu['template_id'] = $row['template_id'];
		$menu['name'] = $row['name'];
		$menu['type'] = $row['type'];
		$menu['level'] = $row['level'];
	}
	$menu['level']++;
	
	$insert  = 'INSERT INTO '.DB_PERFIX.'menu_templates (`template_id`, `name`, `type`, `level`)'; 
	$insert .= 'VALUES ('.$id.', "'.$menu['name'].'", '.$menu['type'].', '.$menu['level'].');';
	$result = mysql_query($insert);
	echo $insert;
	return $result;
}	
function getMenuTemplate($id)
{
	$result = mysql_query('SELECT * FROM '.DB_PERFIX.'menu_templates WHERE id='.$id.' LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{
		$item['max_levels'] = 0; 
		$item['id'] = $row['id'];
		$item['template_id'] = $row['template_id'];
		$item['name'] = $row['name'];
		$item['type'] = $row['type'];
		$item['level'] = $row['level'];
		$item['height'] = $row['height'];
		$item['image_l'] = $row['image_l'];
		$item['image_r'] = $row['image_r'];
		$item['image'] = $row['image'];
		$item['image_bg'] = $row['image_bg'];
		$item['image_hov'] = $row['image_hov'];
		$item['image_act'] = $row['image_act'];
		$item['style'] = $row['style'];
		$item['active'] = $row['active'];
		//Top menu std
		if(($item['type'] == 0) && ($item['level'] == 0))
		{ $item['max_levels'] = 1; }
		//Side menu std
		if(($item['type'] == 1) && ($item['level'] == 0))
		{ $item['max_levels'] = 1; }
		//Top menu fixed
		if(($item['type'] == 2) && ($item['level'] == 0))
		{ $item['max_levels'] = 30; }
	}
	
	if($item['template_id'] > 0)
	{
		$result = mysql_query('SELECT * FROM '.DB_PERFIX.'menu_templates WHERE template_id='.$item['template_id'].' AND level > '.$item['level'].';');
		while($row = mysql_fetch_array($result))
		{
			$item['child_id'] = $row['id'];
		}
	}
	return $item;
	
}
function setMenuTempType($id, $value)
{
	$update = mysql_query('UPDATE '.DB_PERFIX.'menu_templates SET type='.$value.' WHERE id='.$id.' LIMIT 1;');
	return $update;
}

function setMenuTempName($id, $value)
{
	$update = 'UPDATE '.DB_PERFIX.'menu_templates SET name="'.$value.'" WHERE id='.$id.' LIMIT 1;';
	$update = mysql_query($update);
	return $update;
}

function setMenuTempContType($id, $value)
{
	$update = mysql_query('UPDATE '.DB_PERFIX.'menu_templates SET cont_type='.$value.' WHERE id='.$id.' LIMIT 1;');
	return $update;
}

function deleteMenuTemp($id)
{
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'menu_templates WHERE id='.$id.';');
	return $result;
}
?>